<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class SearchController extends Controller {

    private $_indexFiles = 'runtime.search';
    /**
     * (non-PHPdoc)
     * @see CController::init()
     */
    public function init(){
         Yii::import('application.vendors.*');
         Yii::setPathOfAlias('Zend',Yii::getPathOfAlias('application.vendors.zend'));
        require_once('Zend/Search/Lucene.php');;
        parent::init(); 
    }
    
    public function actionCreate()
    {
       
        
        $index = new Zend_Search_Lucene(Yii::getPathOfAlias('application.' . $this->_indexFiles), true);
 
        $posts = Service::model()->findAll();
       $res=  Homemade::model()->findAll();
        
        foreach($posts as $post){
            $doc = new Zend_Search_Lucene_Document();

            $doc->addField(Zend_Search_Lucene_Field::Text('title',
                                          CHtml::encode($post->title), 'utf-8')
            );
 
            $doc->addField(Zend_Search_Lucene_Field::Text('link',
                                            CHtml::encode($post->id)
                                                , 'utf-8')
            );  
 
            $doc->addField(Zend_Search_Lucene_Field::Text('description',
                                          CHtml::encode($post->description)
                                          , 'utf-8')
            );
 
 
            $index->addDocument($doc);
        }
        foreach($res as $r){
            $doc = new Zend_Search_Lucene_Document();

            $doc->addField(Zend_Search_Lucene_Field::Text('title',
                                          CHtml::encode($r->title), 'utf-8')
            );
 
            $doc->addField(Zend_Search_Lucene_Field::Text('link',
                                            CHtml::encode($r->id)
                                                , 'utf-8')
            );  
 
            $doc->addField(Zend_Search_Lucene_Field::Text('description',
                                          CHtml::encode($r->description)
                                          , 'utf-8')
            );
 
 
            $index->addDocument($doc);
        }
        $index->commit();
        echo 'Lucene index created';
    }
    
    public function actionSearch()
    {
        $this->layout='column1';
         if (($term = Yii::app()->getRequest()->getParam('q', null)) !== null) {
            $index = new Zend_Search_Lucene(Yii::getPathOfAlias('application.' . $this->_indexFiles));
            $results = $index->find($term);
            $query = Zend_Search_Lucene_Search_QueryParser::parse($term);       
            
            $this->render('search', compact('results', 'term', 'query'));
        }
    }
    
    public function filters() {
        return array(
            'rights', // perform access control for CRUD operations
            'postOnly + delete', // we only allow deletion via POST request
        );
    }

    public function allowedActions() {
        return 'index,create,search';
    }

    public function actionIndex() {
        $lang = Yii::app()->getLanguage();
        if (!isset($_POST['query'])) {
            $_POST['query'] = '';
        }

        if ($lang == 'pt') {
            $res = Yii::app()->db->createCommand()
                    ->select('*')
                    ->from('real_estate')
                    ->where('activ=1 and(title LIKE "%' . $_POST['query'] . '%" or description LIKE "%' . $_POST['query'] . '%" )')
            ;
            $countRes = Yii::app()->db->createCommand()
                    ->select('*')
                    ->from('real_estate')
                    ->where('activ=1 and(title LIKE "%' . $_POST['query'] . '%" or description LIKE "%' . $_POST['query'] . '%" )')
                    ->queryAll();
            
        } else {
            $lang = Yii::app()->getLanguage() == 'pt' ? '' : Yii::app()->getLanguage();
            $res = Yii::app()->db->createCommand()
                    //->select('id,title,img1')
                    ->selectDistinct('real_estate.id,title,img1')
                    ->from('real_estate')
                    ->leftJoin('SourceMessage', '((real_estate.title = SourceMessage.message) or (real_estate.description = SourceMessage.message))')
                    ->leftJoin('Message', 'SourceMessage.id = Message.id')
                    ->where('(Message.language="' . $lang . '"  And Message.translation LIKE "%' . $_POST['query'] . '%" and real_estate.activ=1)')
            ;
            
            $countRes = Yii::app()->db->createCommand()
                //->select('id,title,img1')
                ->selectDistinct('real_estate.id,title,img1')
                ->from('real_estate')
                ->leftJoin('SourceMessage', '((real_estate.title = SourceMessage.message) or (real_estate.description = SourceMessage.message))')
                ->leftJoin('Message', 'SourceMessage.id = Message.id')
                ->where('(Message.language="' . $lang . '"  And Message.translation LIKE "%' . $_POST['query'] . '%" and real_estate.activ=1)')
                ->queryAll();
        }
        
        $res = new CSqlDataProvider($res, array('keyField' => 'id',
            'totalItemCount'=>count($countRes),
            'pagination'=>array(
        'pageSize'=>5,
        )));

        if ($lang == 'pt') {
            $resBusiness = Yii::app()->db->createCommand()
                    ->select('*')
                    ->from('business')
                    ->where(' activ=1 and (title LIKE "%' . $_POST['query'] . '%" or descripton LIKE "%' . $_POST['query'] . '%") ')
            ;
            $countBus= Yii::app()->db->createCommand()
                    ->select('*')
                    ->from('business')
                    ->where(' activ=1 and (title LIKE "%' . $_POST['query'] . '%" or descripton LIKE "%' . $_POST['query'] . '%") ')
            ->queryAll();
        } else {
            $lang = Yii::app()->getLanguage() == 'pt' ? '' : Yii::app()->getLanguage();
            $resBusiness = Yii::app()->db->createCommand()
                    ->selectDistinct('business.id,title,img1')
                    ->from('business')
                    ->leftJoin('SourceMessage', '(business.title = SourceMessage.message or business.descripton = SourceMessage.message)')
                    ->leftJoin('Message', 'SourceMessage.id = Message.id')
                    ->where('(Message.language="' . $lang . '"  And Message.translation LIKE "%' . $_POST['query'] . '%" and business.activ=1)')
            ;
            $countBus=Yii::app()->db->createCommand()
                    ->selectDistinct('business.id,title,img1')
                    ->from('business')
                    ->leftJoin('SourceMessage', '(business.title = SourceMessage.message or business.descripton = SourceMessage.message)')
                    ->leftJoin('Message', 'SourceMessage.id = Message.id')
                    ->where('(Message.language="' . $lang . '"  And Message.translation LIKE "%' . $_POST['query'] . '%" and business.activ=1)')
                    ->queryAll();
        }
        $resBusiness = new CSqlDataProvider($resBusiness, array('keyField' => 'id',
            'totalItemCount'=>count($countBus),
            'pagination'=>array(
        'pageSize'=>5,
        )));
        if ($lang == 'pt') {
            $resHomemade = Yii::app()->db->createCommand()
                    ->select('*')
                    ->from('homemade')
                    ->where('activ=1 and (title LIKE "%' . $_POST['query'] . '%" or description LIKE "%' . $_POST['query'] . '%")')
            ;
            $countHm=Yii::app()->db->createCommand()
                    ->select('*')
                    ->from('homemade')
                    ->where('activ=1 and (title LIKE "%' . $_POST['query'] . '%" or description LIKE "%' . $_POST['query'] . '%")')
            ->queryAll();
        } else {
            $lang = Yii::app()->getLanguage() == 'pt' ? '' : Yii::app()->getLanguage();
            $resHomemade = Yii::app()->db->createCommand()
                    ->selectDistinct('homemade.id,title,img1')
                    ->from('homemade')
                    ->leftJoin('SourceMessage', '(homemade.title = SourceMessage.message or homemade.description = SourceMessage.message)')
                    ->leftJoin('Message', 'SourceMessage.id = Message.id')
                    ->where('(Message.language="' . $lang . '"  And Message.translation LIKE "%' . $_POST['query'] . '%" and homemade.activ=1)')
            ;
            $countHm= Yii::app()->db->createCommand()
                    ->selectDistinct('homemade.id,title,img1')
                    ->from('homemade')
                    ->leftJoin('SourceMessage', '(homemade.title = SourceMessage.message or homemade.description = SourceMessage.message)')
                    ->leftJoin('Message', 'SourceMessage.id = Message.id')
                    ->where('(Message.language="' . $lang . '"  And Message.translation LIKE "%' . $_POST['query'] . '%" and homemade.activ=1)')
                    ->queryAll();
        }
        $resHomemade = new CSqlDataProvider($resHomemade, array('keyField' => 'id',
            'totalItemCount'=>count($countHm),
            'pagination'=>array(
        'pageSize'=>5,
        )));
        
        if ($lang == 'pt') {
            $resService = Yii::app()->db->createCommand()
                    ->select('*')
                    ->from('service')
                    ->where(' activ=1 and (title LIKE "%' . $_POST['query'] . '%" or description LIKE "%' . $_POST['query'] . '%" )')
            ;
            $countServ=Yii::app()->db->createCommand()
                    ->select('*')
                    ->from('service')
                    ->where(' activ=1 and (title LIKE "%' . $_POST['query'] . '%" or description LIKE "%' . $_POST['query'] . '%" )')
            ->queryAll();
        } else {
            $lang = Yii::app()->getLanguage() == 'pt' ? '' : Yii::app()->getLanguage();
            $resService = Yii::app()->db->createCommand()
                    ->selectDistinct('service.id,title,img1')
                    ->from('service')
                    ->leftJoin('SourceMessage', '(service.title = SourceMessage.message or service.description = SourceMessage.message)')
                    ->leftJoin('Message', 'SourceMessage.id = Message.id')
                    ->where('(Message.language="' . $lang . '"  And Message.translation LIKE "%' . $_POST['query'] . '%" and service.activ=1)')
            ;
            $countServ=Yii::app()->db->createCommand()
                    ->selectDistinct('service.id,title,img1')
                    ->from('service')
                    ->leftJoin('SourceMessage', '(service.title = SourceMessage.message or service.description = SourceMessage.message)')
                    ->leftJoin('Message', 'SourceMessage.id = Message.id')
                    ->where('(Message.language="' . $lang . '"  And Message.translation LIKE "%' . $_POST['query'] . '%" and service.activ=1)')
           ->queryAll() ;
        }
        $resService = new CSqlDataProvider($resService, array('keyField' => 'id',
            'totalItemCount'=>count($countServ),
            'pagination'=>array(
        'pageSize'=>5,
            
    
        )));
        $this->render('//site/search', array(
            'res' => $res,
            'resBusiness' => $resBusiness,
            'resHomemade' => $resHomemade,
            'resService' => $resService,
            
        ));
    }

}

?>
